﻿Imports System.Data.SqlClient

Public Class Chargement

    Private user As String
    Private userSite As String
    Private cn As CnData
    Private da As SqlDataAdapter
    Private ds As DataSet
    Private cmd As SqlCommand
    Private dtr As SqlDataReader
    Private InfoSpe As Boolean
    Private compteur As Integer

    Public Sub New(ByVal user As String, ByRef cn As CnData, ByVal userSite As String)
        ' Cet appel est requis par le concepteur.
        InitializeComponent()

        ' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
        Me.user = user
        Me.cn = cn
        Me.userSite = userSite

    End Sub

    Private Sub Chargement_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        cn.Close()
    End Sub

    Private Sub Chargement_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        cn.Open()
        cmd = New SqlCommand("SELECT NomDoc FROM Sites WHERE NumSite='" & userSite & "'", cn.getCn)
        SiteE.Text = cmd.ExecuteScalar
        'MsgBox(SiteE.Text)
        da = New SqlDataAdapter("SELECT * FROM Sites WHERE Actif = 1", cn.getCn)
        ds = New DataSet
        da.Fill(ds, "SitesD")
        With SiteD
            .DataSource = ds.Tables("SitesD")
            .DisplayMember = "NomDoc"
            .ValueMember = "NumSite"
            .SelectedIndex = 0
        End With
        da.Fill(ds, "SitesT")
        da = New SqlDataAdapter("SELECT * FROM Transporteurs", cn.getCn)
        da.Fill(ds, "Transp")
        With Transp
            .DataSource = ds.Tables("Transp")
            .DisplayMember = "NomTransporteur"
            .ValueMember = "IdTransporteur"
        End With

        da = New SqlDataAdapter("SELECT * FROM Chauffeurs WHERE IdTransporteur = " & Transp.SelectedValue, cn.getCn)
        da.Fill(ds, "Chauf")
        With Chauf
            .DataSource = ds.Tables("Chauf")
            .DisplayMember = "NomChauffeur"
            .ValueMember = "NomChauffeur"
        End With

        da = New SqlDataAdapter("SELECT * FROM Navettes WHERE IdTransporteur = " & Transp.SelectedValue, cn.getCn)
        da.Fill(ds, "Immat")
        With Immat
            .DataSource = ds.Tables("Immat")
            .DisplayMember = "Immatriculation"
            .ValueMember = "Immatriculation"
        End With

        cn.Close()
    End Sub

    Private Sub Transp_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Transp.LostFocus
        If Transp.SelectedValue = Nothing Then
            Try
                ds.Tables("Immat").Clear()
            Catch ex As Exception
            End Try
        End If
    End Sub

    Private Sub Transp_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Transp.SelectedIndexChanged
        If Transp.ValueMember = "" Then
            Exit Sub
        End If
        cn.Open()
        da = New SqlDataAdapter("SELECT * FROM Navettes WHERE IdTransporteur = " & Transp.SelectedValue, cn.getCn)
        Try
            If Not IsNothing(ds.Tables("Immat")) Then ds.Tables("Immat").Clear()
        Catch ex As Exception
        End Try
        da.Fill(ds, "Immat")
        With Immat
            .DataSource = ds.Tables("Immat")
            .DisplayMember = "Immatriculation"
            .ValueMember = "Immatriculation"
        End With

        da = New SqlDataAdapter("SELECT * FROM Chauffeurs WHERE IdTransporteur = " & Transp.SelectedValue, cn.getCn)
        Try
            If Not IsNothing(ds.Tables("Chauf")) Then ds.Tables("Chauf").Clear()
        Catch ex As Exception
        End Try
        da.Fill(ds, "Chauf")
        With Chauf
            .DataSource = ds.Tables("Chauf")
            .DisplayMember = "NomChauffeur"
            .ValueMember = "NomChauffeur"
        End With

        cn.Close()
    End Sub

    Private Sub Fermer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Fermer.Click
        Me.Close()
    End Sub

    Private Sub Scan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Scan.Click
        If String.IsNullOrEmpty(Transp.Text) Or String.IsNullOrEmpty(Immat.Text) Or String.IsNullOrEmpty(Chauf.Text) Then
            MsgBox("Veuillez remplir les champs obligatoires")
            Exit Sub
        End If
        cn.Open()
        cmd = New SqlCommand("SELECT Compteur FROM Sites WHERE NumSite='" & userSite & "'", cn.getCn)
        compteur = cmd.ExecuteScalar()
        cmd = New SqlCommand("UPDATE Sites SET Compteur=Compteur+1 WHERE NumSite='" & userSite & "'", cn.getCn)
        cmd.ExecuteNonQuery()
        Dim chargement As String
        chargement = ("0" + userSite.ToString).Substring(("0" + userSite.ToString).Length - 2)
        chargement = chargement + ("0" + SiteD.SelectedValue.ToString).Substring(("0" + SiteD.SelectedValue.ToString).Length - 2)
        chargement = chargement + Format(Date.Today, "ddMMyy")
        chargement = chargement + ("000" + compteur.ToString).Substring(("000" + compteur.ToString).Length - 4)
        chargement = chargement + ("00" + cle(chargement).ToString).Substring(("00" + cle(chargement).ToString).Length - 2)

        cmd = New SqlCommand("INSERT INTO Chargements (Chargement,Expediteur,Destinataire,Statut,Plomb1,Plomb2,Transporteur,Immatriculation,Chauffeur,NbColis,NbAgres,UtilCre,DateCre) VALUES ('" & chargement & "','" & userSite & "','" & SiteD.SelectedValue & "','En chargement','" & Plomb1.Text & "','" & Plomb2.Text & "','" & Transp.Text & "','" & Immat.Text & "','" & Chauf.Text & "',0,0,'" & user & "','" & Date.Today & "')", cn.getCn)
        cmd.ExecuteNonQuery()
        cn.Close()

        Dim ChildForm As New Expedition(user, cn, chargement, False)
        If ChildForm.ShowDialog() = Windows.Forms.DialogResult.OK Then Me.Close()
    End Sub

    Private Function cle(ByVal num As String)
        Return (CInt(num.Substring(0, 2)) + CInt(num.Substring(2, 2)) + CInt(num.Substring(4, 2)) + CInt(num.Substring(6, 2)) + CInt(num.Substring(8, 2)) + CInt(num.Substring(10, 2)) + CInt(num.Substring(12, 2))) Mod 100
    End Function
End Class